package com.egao.common.module.company.service.impl;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.egao.common.core.exception.BusinessException;
import com.egao.common.core.exception.ParameterException;
import com.egao.common.core.web.JsonResult;
import com.egao.common.core.web.PageParam;
import com.egao.common.module.company.entity.FieldRecord;
import com.egao.common.module.company.entity.vo.FieldRecordFormVO;
import com.egao.common.module.company.mapper.FieldMapper;
import com.egao.common.module.company.mapper.FieldRecordMapper;
import com.egao.common.module.company.service.FieldRecordService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

import static javax.management.Query.eq;

/**
 * 场地预约记录(FieldRecord)表服务实现类
 *
 * @author dl
 */
@Service
public class FiledRecordServiceImpl extends ServiceImpl<FieldRecordMapper, FieldRecord> implements FieldRecordService {

    @Resource
    private FieldRecordMapper fieldRecordMapper;

    @Override
    public JsonResult saveFieldRecord(FieldRecordFormVO fieldRecordFormVO) {
        if (fieldRecordFormVO == null) {
            throw new ParameterException("场地预约记录为空");
        }

        FieldRecord fieldRecord = new FieldRecord();
        BeanUtil.copyProperties(fieldRecordFormVO, fieldRecord);

        if (save(fieldRecord)) {
            return JsonResult.ok("提交成功");
        }
        return JsonResult.error("提交失败");
    }

    @Override
    public JsonResult deleteFieldRecord(FieldRecordFormVO fieldRecordFormVO) {
        FieldRecord fieldRecord = verifyFormData(fieldRecordFormVO);

        if (removeById(fieldRecord)) {
            return JsonResult.ok("删除成功");
        }
        return JsonResult.error("删除失败");
    }

    @Override
    public List<FieldRecordFormVO> listRecord(PageParam<FieldRecordFormVO> page) {
        return baseMapper.listRecord(page);
    }

    @Override
    public List<FieldRecordFormVO> queryDayListRecord(PageParam<FieldRecordFormVO> page) {
        return fieldRecordMapper.queryDayListRecord(page);
    }

    @Override
    public JsonResult lockFieldRecord(FieldRecordFormVO fieldRecordFormVO) {
        if (fieldRecordFormVO == null) {
            throw new ParameterException("场地预约记录为空");
        }

        /**
         * 判断当前是分配还是锁定
         * 如果是分配需要检查当前企业当天是否已经分配
         */
        if (1 == (fieldRecordFormVO.getType())) {
            LambdaQueryWrapper<FieldRecord> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(FieldRecord::getCompanyId, fieldRecordFormVO.getCompanyId())
                    .eq(FieldRecord::getStartDate, fieldRecordFormVO.getStartDate());
            if (fieldRecordMapper.selectOne(queryWrapper) != null) {
                throw new BusinessException("当前企业当天已经分配场地，不能重复分配");
            }
        }

        // 查询start_time,end_time,start_date相同的预约记录
        LambdaQueryWrapper<FieldRecord> qw1 = new LambdaQueryWrapper<>();
        qw1.eq(FieldRecord::getStartTime, fieldRecordFormVO.getStartTime())
                .eq(FieldRecord::getFieldId, fieldRecordFormVO.getFieldId())
                .eq(FieldRecord::getEndTime, fieldRecordFormVO.getEndTime())
                .eq(FieldRecord::getStartDate, fieldRecordFormVO.getStartDate());
        int count1 = count(qw1);
        if (count1 > 0) {
            // 存在相同时间段的记录，无法保存
            throw new BusinessException("已存在相同时间段的预约记录");
        }
        /**
         * 查询是否有冲突的时间段
         * 1. 预约的时间段包含start_time
         * 2. 预约的时间段包含end_time
         * 3. 预约的时间段同时小于start_time和end_time
         */
        LambdaQueryWrapper<FieldRecord> qw2 = new LambdaQueryWrapper<>();
        qw2.eq(FieldRecord::getFieldId, fieldRecordFormVO.getFieldId())
                .eq(FieldRecord::getStartDate, fieldRecordFormVO.getStartDate())
                .and(
                        wrapper -> wrapper.lt(FieldRecord::getStartTime, fieldRecordFormVO.getStartTime())
                                .gt(FieldRecord::getEndTime, fieldRecordFormVO.getEndTime())
                                .or()
                                .between(FieldRecord::getStartTime, fieldRecordFormVO.getStartTime(), fieldRecordFormVO.getEndTime())
                                .or()
                                .between(FieldRecord::getEndTime, fieldRecordFormVO.getStartTime(), fieldRecordFormVO.getEndTime())
                                .or()
                                .gt(FieldRecord::getStartTime, fieldRecordFormVO.getStartTime())
                                .lt(FieldRecord::getEndTime, fieldRecordFormVO.getStartTime())
                );


        int count2 = count(qw2);
        if (count2 > 0) {
            // 存在相同时间段的记录，无法保存
            throw new BusinessException("存在冲突的分配或锁定的记录");
        }

        FieldRecord fieldRecord = new FieldRecord();
        BeanUtil.copyProperties(fieldRecordFormVO, fieldRecord);
        if (save(fieldRecord)) {
            return JsonResult.ok("提交成功");
        }
        return JsonResult.error("提交失败");
    }


    /**
     * 检验修改数据
     *
     * @param fieldRecordFormVO 场地表单
     * @return FieldRecord 场地记录
     */
    private FieldRecord verifyFormData(FieldRecordFormVO fieldRecordFormVO) {
        if (fieldRecordFormVO == null) {
            throw new ParameterException("场地为空");
        }

        if (fieldRecordFormVO.getId() == null) {
            throw new ParameterException("场地编号为空");
        }
        FieldRecord fieldRecord = getById(fieldRecordFormVO.getId());
        if (fieldRecord == null) {
            throw new BusinessException("场地为空");
        }
        return fieldRecord;
    }
}
